package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ShouldPayListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ShouldGathPayList;
import com.isunland.managesystem.entity.ShouldGathPayList.GatheringDetail;
import com.isunland.managesystem.entity.ShouldGgatherPay;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 应付款列表
 *
 * @author wu
 */
public class ShouldPayListFragment extends BaseListFragment {

  private static final int REQUEST_QUERY = 0;
  private ShouldPayListAdapter mAdapter;
  private ShouldGgatherPay mQuery = new ShouldGgatherPay();
  private int mPage = 1;// 请求第几页的数据.

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置返回图标
    setBack(true);
    Date date = MyDateUtil.getBeforeMonth(6);
    String startdate = MyDateUtil.dateToString(date, MyDateUtil.TYPE_DATE);
    String endDate = MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE);
    mQuery.setStratTime(startdate);
    mQuery.setEndTime(endDate);
    mQuery.setPartId("");
    mQuery.setPartName("");
    mQuery.setPartPay("T");
    mQuery.setNotPay("T");
    mQuery.setHaspay("T");
    mQuery.setContractCode("");

  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(Mode.BOTH);
  }

  @Override public String getUrl() {

    return ApiConst.URL_GATHER_PAY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("page", mPage + "");
    params.put("rows", "20");
    params.put("flag", "out");

    params.put("beginregDate", mQuery.getStratTime());
    params.put("endregDate", mQuery.getEndTime());
    params.put("partId", mQuery.getPartId());
    params.put("partName", mQuery.getPartName());
    params.put("jsbf", mQuery.getPartPay());
    params.put("wjs", mQuery.getNotPay());
    params.put("yjs", mQuery.getHaspay());
    params.put("contractCode", mQuery.getContractCode());
    return params;
  }

  @Override public void resolveResponse(String json) {
    ShouldGathPayList list = new Gson().fromJson(json, ShouldGathPayList.class);
    List<GatheringDetail> detail = list.getRows();
    if (detail != null && detail.size() == 0) {
      ToastUtil.show(R.string.that_is_all);
    }
    setAdapter(detail);
  }

  private void setAdapter(List<GatheringDetail> detail) {
    if (detail == null) {
      detail = new ArrayList<>();
    }

    if (mAdapter == null) {
      mAdapter = new ShouldPayListAdapter(getActivity(), detail);
      setListAdapter(mAdapter);
      return;
    }
    if (mPage == 1) {
      mAdapter.clear();
    }
    mAdapter.addAll(detail);
    ((ShouldPayListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    GatheringDetail detail = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), ShouldGatherPayDetailActivity.class);
    intent.putExtra(ShouldGatherPayDetailFragment.EXTRA_PAY_VALUE, detail);
    intent.putExtra(ShouldGatherPayDetailFragment.EXTRA_TYPE,
        ShouldGatherPayDetailFragment.EXTRA_PAY);
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query_contract, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_query:
        intent = new Intent(getActivity(), ShouldGatherPaySearchActivity.class);
        intent.putExtra(ShouldGatherPaySearchFragment.EXTRA_TYPE,
            ShouldGatherPaySearchFragment.EXTRA_PAY);
        if (mQuery != null) {
          intent.putExtra(ShouldGatherPaySearchFragment.EXTRA_VALUE, mQuery);
        }
        startActivityForResult(intent, REQUEST_QUERY);
        break;

      case R.id.menu_item_contract:
        intent = new Intent(getActivity(), ShouldPayCountListActivity.class);
        intent.putExtra(ShouldCountListFragment.EXTRA_TYPE, ShouldCountListFragment.VALUE_PAY);
        intent.putExtra(ShouldCountListFragment.EXTRA_PAY_VALUE, mQuery);
        startActivity(intent);
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_QUERY && data != null) {
      mQuery = (ShouldGgatherPay) data.getSerializableExtra(
          ShouldGatherPaySearchFragment.EXTRA_QUERY_VALUE);
      LogUtil.i("mPartId====" + mQuery.getPartId());
    }
    mPullRefreshListView.setMode(Mode.PULL_FROM_START);
    mPage = 1;
    volleyPost();
    mPullRefreshListView.setMode(Mode.BOTH);
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {

    Mode mode = refreshView.getCurrentMode();
    if (mode.equals(Mode.PULL_FROM_START)) {
      mPage = 1;
    }
    if (mode.equals(Mode.PULL_FROM_END)) {
      mPage++;
    }
    volleyPost();
  }
}
